import $ from './libs/jquery';
import './common/xlogin';
//回到顶部
(function(){
    var FeedbackOptions=FeedbackOptions||{};
    FeedbackOptions.style=FeedbackOptions.style||'cms';
    FeedbackOptions.host=FeedbackOptions.host||'info.xcar.com.cn';
    var className='return_coat';
    //if(document.body.clientWidth<1140)return;
    var htmlStr='<!-- 右侧随动 -->\
	<!-- 右侧随动 判断页面宽度为1024时，修改下面#control_coat的class修改为“control_coat1024” -->\
	<div class="'+className+'" style="display:none;" id="control_coat">\
	<div class="return_top" id="return_top"><a href="javascript:void(0)">回到顶部</a></div>\
	</div>\
	<!-- /右侧随动 -->';
    document.write(htmlStr);
    var controlCoat=document.getElementById('control_coat')
    window.onscroll=function(){
        var thisTop=document.body.scrollTop+document.documentElement.scrollTop;
        if(thisTop>500){
            controlCoat.style.display="block";

        }else{
            controlCoat.style.display="none";
        }
    }
    function backTop(){
        if(typeof jQuery!=='undefined'){
            jQuery('html,body').animate({scrollTop:0});
        }else{
            window.scrollTo(0,0);
        }
    }
    return_top.onclick=function(){
        backTop();
    }

})()
$(function(){
    $(".goto_top").click(function(){
        $("html,body").animate({scrollTop:0},"600");
        return false;
    });
    $(".mark-btcon .close").click(function(){
        $(".mark-bt").hide();
        $(".mark-btcon").hide();
    })
});

//卡顶
var a = $('.fix_top'),
    b =a.offset();
$(window).scroll(function(){
    var c = $(this).scrollTop();
    if(c>355){
      a.css({'display':'block','position':'fixed','top':'0px'})
      }else{
        a.css({'display':'none','position':'absolute','top':'500px'})
        }
   })
//cookie
var Cookie = {
    getCookie: function (k) {
        var all = document.cookie.split(";");
        var cookieData = window.__cookie__ ={};
        for (var i = 0, l = all.length; i < l; i++) {
            var p = all[i].indexOf("=");
            var dataName = all[i].substring(0, p).replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"");
            cookieData[dataName] = all[i].substring(p + 1);
        }
        return cookieData[k]
    }
};
$(function() {

    $(".follow").each(function(index,item){
        if(Cookie.getCookie("_discuz_uid") == undefined || Cookie.getCookie("_discuz_uid") == null || Cookie.getCookie("_discuz_uid") == ''){
        }
        else{
            if(Cookie.getCookie("_discuz_uid")==$(item).parents('div').find('.pre_head .name').attr('data-uid')){
                $(item).hide()
            }
        }
    })

    //关注
    $('body').on('click','.follow', function(e) {
        e.preventDefault();
        var $self=$(this);
        function AttentionFn(){
            $.ajax({
                url: 'http://rapapi.xcar.com.cn/app/mock/24/POST/ajax/follow',
                data:{
                    suid:$self.parents('div').find('.pre_head .name').attr('data-auid'),
                    // uid:Cookie.getCookie("_discuz_uid"),
                    operation:$self.hasClass('followed')?0:1,
                },
                type: "POST",
                dataType: 'json',
                success: function(res) {
                    if(res.code==200){
                        if(res.result['is_follow']==0){
                            $self.removeClass('followed');
                            $self.html("+关注");
                        }else{
                            $self.addClass('followed');
                            if(res.result['is_follow']==1){
                                $self.html("已关注");
                            }
                            if(res.result['is_follow']==2){
                                $self.html("相互关注");
                            }
                        }
                    }
                }
            });
        }
        if (Cookie.getCookie("_discuz_uid") == undefined || Cookie.getCookie("_discuz_uid") == null || Cookie.getCookie("_discuz_uid") == '') {
            $self.unbind();
            $self.xLoginBox({
                func: function() {
                    callLoginScript();
                    AttentionFn();
                    location.reload()
                },
                show: true,
                step: 2
            });
        }else{
            AttentionFn()
        }
    })

    //收藏
    $('body').on('click','.collect', function(e) {
        e.preventDefault();
        var $self=$(this);
        function CollectFn(){
            $.ajax({
                url: 'http://rapapi.xcar.com.cn/app/mock/24/POST/ajax/collect',
                data:{
                    qid:$self.attr('data-qid'),
                    operation:$self.hasClass('collected')?0:1,
                },
                type: "POST",
                dataType: 'json',
                success: function(res) {
                    if(res.code==200){

                            if($self.hasClass('collected')){
                                $self.html("收藏问题");
                                $self.removeClass('collected');
                            }else{
                                $self.html("已收藏");
                                $self.addClass('collected');
                            }


                    }

                }
            });
        }
        if (Cookie.getCookie("_discuz_uid") == undefined || Cookie.getCookie("_discuz_uid") == null || Cookie.getCookie("_discuz_uid") == '') {
            $self.unbind();
            $self.xLoginBox({
                func: function() {
                    callLoginScript();
                    CollectFn();
                    location.reload()
                },
                show: true,
                step: 2
            });
        }else{
            CollectFn()
        }
    })

    //点赞
    $('body').on('click','.like', function(e) {
        e.preventDefault();
        var $self=$(this);
        function LikeFn(){
            $.ajax({
                url: 'http://rapapi.xcar.com.cn/app/mock/24/POST/ajax/praise',
                data:{
                    type:2 ,
                    qid:$self.attr('data-qid'),
                    aid:$self.attr('data-aid'),
                    fuid:$self.attr('data-auid'),
                    operation:$self.hasClass('liked')?0:1,
                },
                type: "POST",
                dataType: 'json',
                success: function(res) {
                    if(res.code==200){

                        if($self.hasClass('liked')){
                            return;
                        }else{
                            $self.addClass('liked');
                            var like_count=parseInt($self.find("em").html()===''?0:$self.find("em").html());
                            like_count++;
                            $self.find("em").html(like_count);
                        }
                    }
                }
            });
        }
        if (Cookie.getCookie("_discuz_uid") == undefined || Cookie.getCookie("_discuz_uid") == null || Cookie.getCookie("_discuz_uid") == '') {
            $self.unbind();
            $self.xLoginBox({
                func: function() {
                    callLoginScript();
                    location.reload()
                },
                show: true,
                step: 2
            });
        }else{
            LikeFn()
        }
    })

    //最佳答案
    $('body').on('click','.set_best_ans .blue', function(e) {
        e.preventDefault();
        var $self=$(this),
            _qid=$self.attr('data-qid'),
            _aid=$self.attr('data-aid'),
            _operation=$('.answer_list').find('li:eq('+curIndex+')').find('.answer_btn').hasClass('active')?0:1;

            BestFn(_qid,_aid,_operation,function(res){
                if(res.code == 200){
                   $('.answer_list').find('li:eq('+curIndex+')').find('.answer_btn').addClass('active').text('最佳答案').css("display","block").parents('li').siblings('li').find('.answer_btn').remove();
                    $("#graybg_pop").hide();
                    $(".set_best_ans").hide();
                    // location.reload();
                }
            });
    })
    //设为最佳
    function BestFn(qid,aid,opera,callback){
        $.ajax({
            url: 'http://rapapi.xcar.com.cn/app/mock/24/POST/ajax/optimum',
            data:{
                qid:qid,
                aid:aid,
                operation:opera
            },
            type: "POST",
            dataType: 'json',
            success: function(res) {
                callback&&callback(res);
            }
        });
    }
    var page=2;
    $('.answer_btn').each(function(index,item){
        if($(item).hasClass('active'))$(item).parents('li').siblings('li').find('.answer_btn').remove();
    })
    //加载更多
    $('body').on('click','.load-btn', function(e) {

        e.preventDefault();
        var $self=$(this);
            $.ajax({
                url: 'http://rapapi.xcar.com.cn/app/mock/24/GET/ajax/answer_list',
                data:{
                    page:page,
                    qid:$self.attr('data-qid')
                },
                type: "POST",
                dataType: 'json',
                success: function(res) {
                    var code=res.code;
                  res=res.result;
                    if(code==200){

                        var html='';
                        for(var i=0;i<res.data.length;i++){
                            html+='<li class="clearfix detail_li">';
                            html+='<dl class="clearfix">';
                            html+='<dt>';
                            html+='<a href="http://my.xcar.com.cn/space.php?uid='+res.data[i].auid+'" data-auid="'+res.data[i].auid+'" target="_blank" class="img"><img src="'+res.data[i].aicon+'" width="34" height="34"></a>';
                            html+='</dt>';
                            html+='<dd class="clearfix">';
                            html+='<div class="clearfix">';
                            html+='<div class="pre_head">';
                            if(res.data[i].is_expert == 1){
                                html+='<a href="http://my.xcar.com.cn/space.php?uid='+res.data[i].auid+'" data-auid="'+res.data[i].auid+'" target="_blank" class="name">'+res.data[i].ausername+'<em></em></a>';
                                html+='<span class="hobby">'+res.data[i].skill+'</span>';
                            }
                            if(res.data[i].is_expert == 0){
                                html+='<a href="http://my.xcar.com.cn/space.php?uid='+res.data[i].auid+'" data-auid="'+res.data[i].auid+'" style="height: 40px; line-height: 40px;" target="_blank" class="name">'+res.data[i].ausername+'</a>';
                            }
                            html+='</div>';
                            if(res.data[i].is_follow==0) {
                                html += '<a href="javascript:void(0)" class="follow">+关注</a>';
                            }
                            if(res.data[i].is_follow==1) {
                                html += '<a href="javascript:void(0)" class="follow followed">已关注</a>';
                            }
                            if(res.data[i].is_follow==2) {
                                html += '<a href="javascript:void(0)" class="follow followed">互相关注</a>';
                            }
                            html+='</div>';
                            html+='<div class="ques_cont_des">'+res.data[i].content+'</div>';
                            html+='<div class="footnote">';
                            Date.prototype.format = function (format) {
                                var args = {
                                    "M+": this.getMonth() + 1,
                                    "d+": this.getDate(),
                                    "h+": this.getHours(),
                                    "m+": this.getMinutes(),
                                    "s+": this.getSeconds(),
                                    "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
                                    "S": this.getMilliseconds()
                                };
                                if (/(y+)/.test(format))
                                    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                                for (var i in args) {
                                    var n = args[i];
                                    if (new RegExp("(" + i + ")").test(format))
                                        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
                                }
                                return format;
                            };
                            var firstcurTime = new Date(res.data[i].ctime*1000).format("yyyy-MM-dd");
                            html+='<span class="time">'+firstcurTime+'</span>';
                            if(res.data[i].is_praises==0){
                                html+='<a href="javascript:void(0)" data-qid="'+$self.attr('data-qid')+'" data-aid="'+res.data[i].aid+'" data-auid="'+res.data[i].auid+'" class="like"><i class="icon"></i><i class="icon2"></i>';
                                    if(res.data[i].praises!=0){
                                        html+='<em>'+res.data[i].praises+'</em>';
                                    }else{
                                        html+='<em></em>';
                                    }
                                html+='</a>';
                            }
                            if(res.data[i].is_praises==1){
                                html+='<a href="javascript:void(0)" data-qid="'+$self.attr('data-qid')+'" data-aid="'+res.data[i].aid+'" data-auid="'+res.data[i].auid+'" class="like liked"><i class="icon"></i><i class="icon2"></i><em>'+res.data[i].praises+'</em></a>';
                            }
                            html+='</div>';
                            if(res.data[i].quote_list.length>0) {
                                for (var j = 0; j < res.data[i].quote_list.length; j++) {
                                    html += '<div class="inner_box">';
                                    html += '<a href="http://my.xcar.com.cn/space.php?uid='+res.data[i].quote_list[j].auid+'" data-auid="'+res.data[i].quote_list[j].auid+'" target="_blank" class="img"><img src="'+res.data[i].quote_list[j].aicon+'" width="34" height="34"></a>';
                                    html += '<div class="clearfix">';
                                    html += '<div class="pre_head">';
                                    if(res.data[i].quote_list[j].is_expert==1) {
                                        html += '<a href="http://my.xcar.com.cn/space.php'+ res.data[i].quote_list[j].auid + '" target="_blank" class="name">'+res.data[i].quote_list[j].ausername+'<em></em></a>';
                                        html+='<span class="hobby">擅长：'+res.data[i].quote_list[j].skill+'</span>';
                                    }
                                    if(res.data[i].quote_list[j].is_expert==0) {
                                        html += '<a href="http://my.xcar.com.cn/space.php'+ res.data[i].quote_list[j].auid + '" style="height:40px;line-height:40px;" target="_blank" class="name">'+res.data[i].quote_list[j].ausername+'</a>';
                                    }

                                    html += '</div>';
                                    html += '</div>';
                                    html += '<div class="ques_cont_des">'+res.data[i].quote_list[j].content+'</div>';
                                    html += '<div class="footnote">';
                                    var secondcurTime = new Date(res.data[i].quote_list[j].ctime*1000).format("yyyy-MM-dd");
                                    html += '<span class="time">'+secondcurTime+'</span>';
                                    if(res.data[i].quote_list[j].is_praises==0) {
                                        html += '<a href="javascript:void(0)"  data-qid="' + $self.attr('data-qid') + '" data-aid="' + res.data[i].quote_list[j].aid + '" data-auid="' + res.data[i].quote_list[j].auid + '" class="like"><i class="icon"></i><i class="icon2"></i>';
                                            if(res.data[i].quote_list[j].praises!=0){
                                                html+='<em>'+ res.data[i].quote_list[j].praises + '</em>';
                                            }else{
                                                html+='<em></em>';
                                            }
                                    }
                                    if(res.data[i].quote_list[j].is_praises==1) {
                                        html += '<a href="javascript:void(0)"  data-qid="' + $self.attr('data-qid') + '" data-aid="' + res.data[i].quote_list[j].aid + '" data-auid="' + res.data[i].quote_list[j].auid + '" class="like liked"><i class="icon"></i><i class="icon2"></i><em>' + res.data[i].quote_list[j].praises + '</em></a>';
                                    }
                                    html += '</div>';
                                    html += '</div>';
                                }
                            }
                            html+='</dd>';
                            html+='</dl>';
                            if(res.data[i].is_good==1) {
                                html += '<a href="javascript:void(0);" data-qid="'+$self.attr('data-qid')+'" data-aid="'+res.data[i].aid+'" target="_blank" class="answer_btn active">最佳答案</a>';
                            }
                            if(res.data[i].is_good==0) {
                                html += '<a href="javascript:void(0);" ddata-qid="'+$self.attr('data-qid')+'" data-aid="'+res.data[i].aid+'" target="_blank" class="answer_btn set_best">设为最佳</a>';
                            }
                            html+='</li>';
                        }
                        page++;
                    }
                    $(".answer_list ul").append(html);
                    $('.answer_btn').each(function(index,item){
                        if($(item).hasClass('active'))$(item).parents('li').siblings('li').find('.answer_btn').remove();
                    })
                }
            });
    })
})
    var curIndex = 0;
    //设为最佳
    $(document).on('mouseover',".detail_li",function() {

        if ($(this).find(".set_best").hasClass("active")) {
            return;
        }
        $(this).find(".set_best").show();
    })
    $(document).on('mouseout',".detail_li",function() {
        if ($(this).find(".set_best").hasClass("active")) {
            return;
        }
        $(this).find(".set_best").hide();
    });

    $(".detail_li .answer_btn").on('click',function(e) {
        e.preventDefault();
        var $self = $(this),
            index = $self.closest('li').index();
        curIndex = index;
        if(!$self.hasClass('active')) {
            if (Cookie.getCookie("_discuz_uid") == undefined || Cookie.getCookie("_discuz_uid") == null || Cookie.getCookie("_discuz_uid") == '') {
                $self.unbind();
                $self.xLoginBox({
                    func: function () {
                        callLoginScript();
                        $("#graybg_pop").show();
                        $(".set_best_ans").show();
                        // location.reload()
                    },
                    show: true,
                    step: 2
                });
            } else {
                $("#graybg_pop").show();
                $(".set_best_ans").show();
            }
        }
    });
    $(".set_best_ans .gray,.set_best_ans .close").click(function(e){
        e.preventDefault();
        $("#graybg_pop").hide();
        $(".set_best_ans").hide();
    });

    //分享
    window._bd_share_config = {
        "common": {
            "bdUrl": "",
            "bdSnsKey": {
                'tsina': '3506466738',
                'tqq': '310088674'
            },
            "bdDes": "xbb",
            "bdText":"" ,
            "bdMini": "2",

            "bdPic":"",
            "bdStyle": "0",
            "bdSize": "32"
        },
        "share": [{
            "tag": "share_2",

            "bdStyle": "0",
            "bdSize": 32
        }]
    };
    /*with(document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];*/


